package com.exonline.backstage.controller.manager;

import com.exonline.backstage.entity.Teacher;
import com.exonline.commons.base.IBaseService;
import com.exonline.commons.dao.DaoImpl;
import com.exonline.commons.dao.IDao;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

public class ManagerService implements IBaseService {
    IDao managerDao = new DaoImpl<>();

    public List queryAll(String sql,int pageSize, int pageNo) throws SQLException {
        return managerDao.queryWithPagingAndConditions(sql,null,pageNo,10,new MapListHandler());
    }
    public List queryAll(int pageSize, int pageNo) throws SQLException {
        return managerDao.queryWithPagingAndConditions("SELECT * FROM userview",null,pageNo,10,new MapListHandler());
    }




    /**
     *  按照名称或id查询一个
     * @author YUKI
     * @date 2024/12/6
     */
    @Override
    public Object queryOne(String field) throws SQLException {
        List<Map<String, Object>> stringMap = (List<Map<String, Object>>) managerDao.queryForObject("SELECT * FROM userview WHERE id = ? OR name like ?", new MapListHandler(), field, field + "%");
        return stringMap;
    }
    /**
     * 执行sql的add命令
     * @author YUKI
     * @date 2024/12/5
     */

    public void add(String sql,Object... obj) throws SQLException {
        managerDao.updateWithTransaction(sql,obj);
    }


    public void update(String sql,Object... obj) throws SQLException {
        managerDao.updateWithTransaction("UPDATE admin SET tname = ?,phone=?,password=? WHERE id = ?",obj);
    }

    @Override
    public void deleteById(String id) throws SQLException {
        managerDao.updateWithTransaction(" DELETE FROM admin WHERE id = ?",id);
    }

    public List<Teacher> queryAllReview(int pageSize, int pageno) throws SQLException {
        List<Teacher> teachers = managerDao.queryWithPagingAndConditions("SELECT * FROM reviewteacherview", null, pageno, pageSize, new BeanListHandler<>(Teacher.class), null);
        return teachers;
    }
    public List<Teacher> queryAllReview(String sql,int pageSize, int pageno,Object...obj) throws SQLException {
        List<Teacher> teachers = managerDao.queryWithPagingAndConditions(sql, null, pageno, pageSize, new BeanListHandler<>(Teacher.class), obj);
        return teachers;
    }


    public void doReview(String id, String tid, String comment, String status) throws SQLException {
        if (status.equals("1")) {
            managerDao.updateWithTransaction("UPDATE teacher set status = 1 where id = ?",tid);
            managerDao.updateWithTransaction("INSERT INTO reviewhistory(id,tid,status,comment) values(?,?,?,?)",id,tid,"1",comment);
        }
        else{
            managerDao.updateWithTransaction("UPDATE teacher set status = 2 where id = ?",tid);
            managerDao.updateWithTransaction("INSERT INTO reviewhistory(id,tid,status,comment) values(?,?,?,?)",id,tid,"2",comment);
        }
    }

    public Teacher queryOneReview(String sql,String id) throws SQLException {
        Teacher teacher = (Teacher) managerDao.queryForObject(sql, new BeanHandler<>(Teacher.class),id);
        return teacher;
    }
}
